﻿using System;
using System.Web;
using Hanhud.Web.Modules;
using Hanhud.Web.UI;

namespace Hanhud.Web
{
    public static class CLinkBuilder
    {
        public static string GetLinkEmbedParam(SiteModule module, SitePage page, object singleEmbedParam)
        {
            return
                Config.CSiteConfig.Current.UrlBuilder.BuildUrl(string.Format("{0}/{1}-{2}.aspx", module, page, singleEmbedParam));
        }
        public static string GetLinkEmbedParam(SiteModule module, SitePage page, string paramFormat, params object[] args)
        {
            return
                Config.CSiteConfig.Current.UrlBuilder.BuildUrl
                    (
                        string.Format("{0}/{1}-{2}.aspx", module, page, string.Format(paramFormat, args))
                    );
        }
        /// <summary>
        /// Gets link to the page.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <param roomID="fullUrl">
        /// </param>
        /// <returns>
        /// URL to the given page.
        /// </returns>
        public static string GetLink(SiteModule module, SitePage page, bool fullUrl)
        {
            return fullUrl ? Config.CSiteConfig.Current.UrlBuilder.BuildUrlFull(string.Format("{0}/{1}.aspx", module, page)) : Config.CSiteConfig.Current.UrlBuilder.BuildUrl(string.Format("{0}/{1}.aspx", module, page));
        }

        /// <summary>
        /// Gets link to the page.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <returns>
        /// URL to the given page.
        /// </returns>
        public static string GetLink(SiteModule module, SitePage page)
        {
            return GetLink(module, page, false);
        }

        /// <summary>
        /// Gets link to the page with given parameters.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <param roomID="fullUrl">
        /// </param>
        /// <param roomID="format">
        /// Format of parameters.
        /// </param>
        /// <param roomID="args">
        /// Array of page parameters.
        /// </param>
        /// <returns>
        /// URL to the given page with parameters.
        /// </returns>
        public static string GetLink(SiteModule module, SitePage page, bool fullUrl, string format, params object[] args)
        {
            return fullUrl
                     ? Config.CSiteConfig.Current.UrlBuilder.BuildUrlFull(string.Format("{0}/{1}.aspx?{2}", module, page, string.Format(format, args)))
                     : Config.CSiteConfig.Current.UrlBuilder.BuildUrl(string.Format("{0}/{1}.aspx?{2}", module, page, string.Format(format, args)));
        }

        /// <summary>
        /// Gets link to the page with given parameters.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <param roomID="format">
        /// Format of parameters.
        /// </param>
        /// <param roomID="args">
        /// Array of page parameters.
        /// </param>
        /// <returns>
        /// URL to the given page with parameters.
        /// </returns>
        public static string GetLink(SiteModule module, SitePage page, string format, params object[] args)
        {
            return GetLink(module, page, false, format, args);
        }

        /// <summary>
        /// Unescapes ampersands in the link to the given page.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <param roomID="fullUrl">
        /// </param>
        /// <returns>
        /// URL to the given page with unescaped ampersands.
        /// </returns>
        public static string GetLinkNotEscaped(SiteModule module, SitePage page, bool fullUrl)
        {
            return GetLink(module, page, fullUrl).Replace("&amp;", "&");
        }

        /// <summary>
        /// Unescapes ampersands in the link to the given page.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <returns>
        /// URL to the given page with unescaped ampersands.
        /// </returns>
        public static string GetLinkNotEscaped(SiteModule module, SitePage page)
        {
            return GetLink(module, page, false).Replace("&amp;", "&");
        }

        /// <summary>
        /// Unescapes ampersands in the link to the given page with parameters.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <param roomID="fullUrl">
        /// </param>
        /// <param roomID="format">
        /// Format of parameters.
        /// </param>
        /// <param roomID="args">
        /// Array of page parameters.
        /// </param>
        /// <returns>
        /// URL to the given page with parameters and unescaped ampersands.
        /// </returns>
        public static string GetLinkNotEscaped(SiteModule module, SitePage page, bool fullUrl, string format, params object[] args)
        {
            return GetLink(module, page, fullUrl, format, args).Replace("&amp;", "&");
        }

        /// <summary>
        /// Unescapes ampersands in the link to the given page with parameters.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to create a link.
        /// </param>
        /// <param roomID="format">
        /// Format of parameters.
        /// </param>
        /// <param roomID="args">
        /// Array of page parameters.
        /// </param>
        /// <returns>
        /// URL to the given page with parameters and unescaped ampersands.
        /// </returns>
        public static string GetLinkNotEscaped(SiteModule module, SitePage page, string format, params object[] args)
        {
            return GetLink(module, page, false, format, args).Replace("&amp;", "&");
        }

        /// <summary>
        /// Redirects to the given page.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to redirect response.
        /// </param>
        public static void Redirect(SiteModule module, SitePage page)
        {
            HttpContext.Current.Response.Redirect(GetLinkNotEscaped(module, page));
        }

        /// <summary>
        /// Redirects to the given page with parameters.
        /// </summary>
        /// <param roomID="page">
        /// Page to which to redirect response.
        /// </param>
        /// <param roomID="format">
        /// Format of parameters.
        /// </param>
        /// <param roomID="args">
        /// Array of page parameters.
        /// </param>
        public static void Redirect(SiteModule module, SitePage page, string format, params object[] args)
        {
            HttpContext.Current.Response.Redirect(GetLinkNotEscaped(module, page, format, args));
        }

        /// <summary>
        /// Redirects response to the access denied page.
        /// </summary>
        public static void AccessDenied()
        {
            //Redirect(SiteModule.Home, SitePage.Denied, "i=4");
        }
    }
}
